﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml.Linq;

namespace CH.Easy.DbProvider.Common
{
    public class ConfigHelper
    {
        static XDocument xdoc;
        static  ConfigHelper() 
        {
            xdoc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + @"Config\Table.xml");
        }
        private static string GetAppSetting(string key) 
        {
            return xdoc.Root.Elements("AppSettings").First().Elements("Add").Where(p => p.Attribute("Key").Value == key).First().Attribute("Value").Value;
           
        }
        private static string GetConnectionString(string key)
        {
            return xdoc.Root.Elements("ConnectionStrings").First().Elements("Add").Where(p => p.Attribute("Name").Value == key).First().Attribute("ConnectionString").Value;
        }

        public static string GetTableConnectionString(string tableName)
        {
            string value = string.Empty;
            try
            {
                 value = GetConnectionString(tableName);
            }
            catch (Exception)
            {
                value = GetConnectionString("Default");
                
            }
            return value;
        }

        public static DbProviderType GetDbProviderType()
        {
            string value = string.Empty;
            try
            {
                value = GetAppSetting("DbProviderType");
            }
            catch (Exception)
            {
                value = "MsSql2005";

            }
            return (DbProviderType)Enum.Parse(typeof(DbProviderType), value, true);
        }


    }
}
